<?
//这里定义一个类brower来判断浏览器种类和操作系统平台
class guestinfo {
	//Our private variables.
	private $browseragent; 
	private $browserversion;
	private $guestos;
	private $guestip;
	//函数来更改相应的变量，平台，浏览器，版本
	private function setagent($newagent) {
		$this->browseragent = $newagent;
	}
	private function setversion($newversion) {
		$this->browserversion = $newversion;
	}
	private function setos($newplatform) {
		$this->guestos = $newplatform;
	}
	private function setip($newip) {
		$this->guestip = $newip;
	}

	//判断返回的字符串提取对应的字符做出判断
	private function determinebrowser () { 
		$agent = $_SERVER['HTTP_USER_AGENT'];
		if (ereg('Maxthon/([0-9].[0-9]{1,2})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("Maxthon");
			//Mozilla/5.0 (Windows; U; Windows NT 5.1) AppleWebKit/528.4 (KHTML, like Gecko) Maxthon/3.0 Safari/528.4 
		} else if (ereg('MAXTHON ([0-9].[0-9]{1,2})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("Maxthon");
			// Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.3; MAXTHON 2.0)
		} else if (ereg('MSIE ([0-9].[0-9]{1,2})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("IE");
		} else if (ereg( 'Opera ([0-9].[0-9]{1,2})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("OPERA");
		} else if (ereg( 'Firefox/([0-9].[0-9]{1,2}.[0-9]{1,2})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("Firefox");
			// Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9
		} else if (ereg( 'Chrome/([0-9].[0-9]{1,3}.[0-9]{1,3})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("Chrome");
			// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/2.0.172.30 Safari/530.5
		} else if (ereg( 'Safari/([0-9]{1,3}.[0-9]{1,3})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("Safari");
		} else if (ereg( 'Mozilla/([0-9].[0-9]{1,2})',$agent,$version)) {
			$this->setversion($version[1]);
			$this->setagent("MOZILLA");
		} else {
			$this->setversion("OoO");
			$this->setagent("Are you MARS?");
		}
	}
	//判断平台的种类
	private function determineplatform() {
		$agent = $_SERVER['HTTP_USER_AGENT'];
		if (eregi('win',$agent) && eregi('nt 5\.1',$agent)) { 
			$this->setos("Windows XP"); 
		} elseif (eregi('win',$agent) && ereg('98',$agent)) {
			$this->setos("Windows 98"); 
		} elseif (eregi('win',$agent) && eregi('nt 5\.0',$agent)) { 
			$this->setos("Windows 2000"); 
		} elseif (eregi('win 9x',$agent) && strpos($agent, '4.90')) {
			$this->setos("Windows ME"); 
		} elseif (eregi('win',$agent) && strpos($agent, '95')) {
			$this->setos("Windows 95");
		} elseif (eregi('win',$agent) && eregi('nt',$agent)) {
			$this->setos("Windows NT");
		} elseif (eregi('win',$agent) && ereg('32',$agent)) {
			$this->setos("Windows 32");
		} elseif (eregi('linux',$agent)) {
			$this->setos("Linux");
		} elseif (eregi('unix',$agent)) {
			$this->setos("Unix");
		} elseif (eregi('ibm',$agent) && eregi('os',$agent)) {
			$this->setos("IBM OS/2");
		} elseif (eregi('NetBSD',$agent)) {
			$this->setos("NetBSD"); 
		} elseif (eregi('BSD',$agent)) {
			$this->setos("BSD"); 
		} elseif (eregi('FreeBSD',$agent)) {
			$this->setos("FreeBSD");
		} else {
			$this->setos("Mars OS?");
		}
	}
	private function determineip() {
		$ip = getenv('REMOTE_ADDR');
		$ip_x = getenv("HTTP_X_FORWARDED_FOR");  
		$ip_c = getenv("HTTP_CLIENT_IP");  
		($ip_x) ? $ip = $ip_x : null ;  
		($ip_c) ? $ip = $ip_c : null ;  
		$this->setip($ip);
	}

	//返回我们想要的信息，浏览器版本以及操作平台
	public function getbrowser() {
		$this->determinebrowser();
		return $this->browseragent . " " . $this->browserversion;
	}
	public function getos() {
		$this->determineplatform();
		return $this->guestos;
	}
	public function getip() {
		$this->determineip();
		return $this->guestip;
	}
}

// End Function utf8_trim($string,$sublen);中文字符截取,这个函数谁写的忘了，拿来用吧.
// ------------------------首页调用 MOD BY Ethan Jiang-20060602-Start---------
function utf8_trim($string, $sublen) {
	$strcut= '';
	if(strlen($string) > $sublen) {
		preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
		for($i=0; $i<count($info[0]); $i++) {
			$strcut .= $info[0][$i];
			$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
			if ($j > $sublen - 3) {
				return $strcut."...";
			}
		}
		return join('', $info[0]);
	} else {
		return $string;
	}
}

?>
